package main

import (
	"fmt"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
)
//定义一个struct，用来和集合对应。
type User struct
{
	Id_ bson.ObjectId `bson:"_id"`
	Name string `bson:"name"`
	Age int `bson:"age"`
	JonedAt time.Time `bson:"joned_at"`
	Interests []string `bson:"interests"`
}


func main() {
	session, err := mgo.Dial("localhost:27017")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	session.SetMode(mgo.Monotonic, true)
	c := session.DB("test").C("users")
	err = c.Insert(&User{ Id_: bson.NewObjectId(),
		Name: "Jimmy Kuu",
		Age: 33,
		JonedAt: time.Now(),
		Interests: []string{"Develop", "Movie"}})
	if err != nil {
		panic(err)
	}
	err = c.Insert(&User{ Id_: bson.NewObjectId(),
		Name: "xiao Ming",
		Age: 18,
		JonedAt: time.Now(),
		Interests: []string{"walk", "Movie"} })
	if err != nil {
		panic(err)
	}
	users := new(User)
	c.Find(bson.M{"name": "Jimmy Kuu"}).One(&users)
	fmt.Println(users)
	/*c.Find(nil).All(&users)
	fmt.Println(users)*/
	/*
	c.Find(bson.M{"$or": []bson.M{bson.M{"name": "Jimmy Kuu"},bson.M{"age": 31}}}).All(&users)
	fmt.Println(users)
	c.Find(bson.M{"$or": []bson.M{bson.M{"name": "Jimmy Kuu"}, bson.M{"age": 31}}}).All(&users)
	fmt.Println(users)
	c.Update(bson.M{"Name": "xiao Ming"}, bson.M{"$pull": bson.M{ "interests": "Golang", }})
	c.Find(bson.M{"name": "xiao Ming"}).All(&users)
	fmt.Println(users)
	c.Remove(bson.M{"name": "Jimmy Kuu"})
	c.Find(nil).All(&users)
	fmt.Println(users)*/

}
